package com.hyq.market.ui.home.gclass;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyq.market.adapter.GoodsClassAdapter;
import com.hyq.market.adapter.SubClassAdapter;
import com.hyq.market.base.IMode;
import com.hyq.market.bean.ClassIndex;
import com.hyq.market.bean.GClass;
import com.hyq.market.bean.Goods;
import com.hyq.market.ui.home.PersenterAdapterListener;
import com.hyq.market.ui.home.home.IViewHome;
import com.hyq.market.ui.home.model.IndexUtil;
import com.hyq.market.ui.home.model.ModelClass;
import com.hyq.market.ui.home.model.ModelGoods;
import com.hyq.market.ui.search.SearchAct;

import java.util.List;

/**
 * Created by Administrator on 2017/7/19 0019.
 */

public class PersenterClass extends PersenterAdapterListener<IViewHome> {
    private IViewClass iView;
    private IMode iMode;

    public PersenterClass(IViewClass iView) {
        this.iView=iView;
    }


    @Override
    public <T> void getData(T t) {
        iMode=new ModelClass();
        iMode.getData(t,this);
    }

    @Override
    public <T> void netSucceed(T t, int code) {
        super.netSucceed(t, code);
        if (t instanceof GClass){
            bindData((GClass) t);
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        onItemChildClick(adapter,view,position);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (adapter instanceof GoodsClassAdapter){
            doGoodsClick(adapter,view,position);
        }
        if (adapter instanceof SubClassAdapter)
        {
            doSubClassClick(adapter,view,position);
        }
    }

    private  void doGoodsClick(BaseQuickAdapter adapter, View view, int position){
        GClass.DataBean.CateListBean cateListBean= (GClass.DataBean.CateListBean) adapter.getData().get(position);
        bindDateSubClass(cateListBean.getCcateList());
        iView.setSubTitle(cateListBean);


    }

    private  void doSubClassClick(BaseQuickAdapter adapter, View view, int position){
        iView.startActivity(SearchAct.class);
    }

    private  void bindData(GClass t) {
        if (t.getData()==null||t.getData().getCateList()==null)
            return;
        iView.getAdapter(1).getData().clear();
        iView.getAdapter(1).getData().addAll(t.getData().getCateList());
        iView.getAdapter(1).notifyDataSetChanged();
        /*设置默认选中第一个选项*/
        onItemClick(iView.getAdapter(1),null,0);

    }


    private void bindDateSubClass(List<GClass.DataBean.CateListBean.CcateListBean> ccateList){
        if (ccateList==null)
            return;
        iView.getAdapter(2).getData().clear();
        iView.getAdapter(2).getData().addAll(ccateList);
        iView.getAdapter(2).notifyDataSetChanged();
    }
}
